package com.srd.phone.activity;

import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.android.volley.VolleyError;
import com.shengrongdai.srd.R;
import com.srd.phone.bean.StaticPageInfo;
import com.srd.phone.mvp.presenters.BasePresenter;
import com.srd.phone.utils.Constants;


public class StaticPageActivity extends BaseActivity {

    private WebView webView;

    private String url = Constants.BASEURL + "staticHtmlApp/staticHtml";

    /**
     * 1表示防控介绍,2表示关于盛融贷，3表示注册协议
     */
    private int getIntentValue = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getIntentValue = getIntent().getIntExtra("flag", -1);
        setContentView(R.layout.activity_static_page);
        initView();
        initData();
    }

    @Override
    public void initView() {
        if (getIntentValue == 1) { //防控介绍
            tv_common_title.setText("防控介绍");
        } else if (getIntentValue == 2) { //关于盛融贷
            tv_common_title.setText("关于盛融贷");
        } else if (getIntentValue == 3) { //注册协议
            tv_common_title.setText("注册协议");
        }
        configWebView();
    }

    private void configWebView() {
        webView = (WebView) findViewById(R.id.wv);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 0) {
                    showWaitingDialog();
                } else if (newProgress == 100) {
                    cancelWaitingDialog();
                }
            }
        });
        WebSettings ws = webView.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setBlockNetworkImage(false);
        webView.requestFocus();
    }

    @Override
    public void initData() {
        super.initData();
        presenter = new BasePresenter(requestQueue, this);
        setUrlArrs(url);
        presenter.requestPostData(url);
    }

    @Override
    public void returnData(String tag, String data) {
        super.returnData(tag, data);
        StaticPageInfo info = StaticPageInfo.parser(data);
        if (getIntentValue == 1) { //防控介绍
            webView.postUrl(info.fkjs, null);
        } else if (getIntentValue == 2) { //关于盛融贷
            webView.postUrl(info.gysrd, null);
        } else if (getIntentValue == 3) { //注册协议
            webView.postUrl(info.zcxy,null);
        }
    }

    @Override
    public void onError(String tag, VolleyError error) {
        super.onError(tag, error);
    }
}
